/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			pgspace.cc
  \date			Aug 2012
  \author		TNick

  \brief		Contains the implementation of PgSpace class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<cpg/cpglibrary.h>
#include	<cpg/logic/cpgdoc.h>
#include	<cpg/logic/layer/pglayglob.h>
#include	<cpg/logic/layer/pglayspc.h>
#include	<cpg/printing/pgprintparams.h>
#include	<cpg/printing/pgprintmanager.h>
#include	<cpg/preferences/prefs.h>

#include	"pgspace.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
PgSpace::PgSpace	( const QString & s_name )
	: GuiderManager()
{
	s_name_ = s_name;
	crt_print_ = 0;
	crt_layer_ = NULL;

	/* set default color */
	clear_col_ = Prefs::getI( Prefs::SPACE_COL_BACK );

	crt_color_ = PgColor::BYLAYER;
	crt_lnpatt_ = PgLnPattern::BYLAYER;;
	crt_lnpatt_scl_ = PgLnPattScale::BYLAYER;;
	crt_weight_ = PgLnWeight::BYLAYER;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgSpace::PgSpace	( CpgDoc * doc, PgSpace * space )
	: GuiderManager()
{
	s_name_ = space->s_name_;

	/* set default color */
	clear_col_ = space->clear_col_;

	/* copy characteristics */
	crt_color_ = PgColor::copy( space->document(), doc, space->crt_color_ );
	crt_lnpatt_ = PgLnPattern::copy( space->document(), doc, space->crt_lnpatt_ );
	crt_lnpatt_scl_ = PgLnPattScale::copy( space->document(), doc, space->crt_lnpatt_scl_ );
	crt_weight_ = PgLnWeight::copy( space->document(), doc, space->crt_weight_ );

	/* printing parameters */
	crt_print_ = space->crt_print_;
	int i_max = space->print_params_.count();
	for ( int i = 0; i < i_max; i++ )
	{
		print_params_.append(
					new PrintParams(
						space->print_params_.at( i )
						)
					);
	}

	/* local layers */
	crt_layer_ = NULL;
	PgLaySpc *	itr_ls;
	PgLaySpc *	new_ls;
	i_max = space->local_ly_lst_.count();
	for ( int i = 0; i < i_max; i++ )
	{
		itr_ls = space->local_ly_lst_.at( i );
		new_ls = new PgLaySpc( this, itr_ls );
		local_ly_lst_.append( new_ls );
		if ( itr_ls == space->crt_layer_ )
			crt_layer_ = new_ls;
	}

	/* global layers */
	i_max = doc->layerCount();
	if ( i_max > 0 )
	{
		glob_ly_list_.reserve( i_max );
		for ( int i = 0; i < i_max; i++ )
			glob_ly_list_.append( NULL );
	}


	/* choose a current layer */
	if ( crt_layer_ == NULL )
	{
		if ( glob_ly_list_.isEmpty() == false )
			crt_layer_ = doc->layer( 0 );
		else if ( local_ly_lst_.isEmpty() == false )
			crt_layer_ = local_ly_lst_.at( 0 );
	}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgSpace::~PgSpace	( void )
{
	foreach( PrintParams * pp, print_params_ )
	{
		delete pp;
	}
	print_params_.clear();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgSpace::removePrintParam		( PrintParams * p )
{
	if ( crt_print_ != -1 )
	{
		PrintParams * crt = print_params_.at( crt_print_ );
		bool b = print_params_.removeOne( p );
		crt_print_ = print_params_.indexOf( crt );
		if ( ( crt_print_ == - 1 ) && ( print_params_.count() > 0 ) )
			crt_print_ = 0;
		return b;
	}
	else
	{
		return print_params_.removeOne( p );
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgSpace *			PgSpace::createDefault			(
		CpgDoc * doc, const QString & s_name )
{
	PgSpace *	ret = new PgSpace( s_name );
	doc->addSpace( ret );

	PrintParams * pp = new PrintParams( ret ); /* sets itself to default values */
	ret->print_params_.append( pp );
	/* trap for future guy who ads default print parameters in constructor */
	Q_ASSERT( ret->print_params_.count() == 1 );
	ret->crt_print_ = 0;

	/* default color was set by the constructor */

	return ret;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgSpace::setName				( const QString & s_new )
{
	if ( assoc_doc_ == NULL )
		return false;

	/** @todo ensure that this is the only space with this name
	PgSpace * trg_s = assoc_doc_->spaceByName( s_new );
	if ( trg_s != NULL )
	{
		return false;
	}*/
	s_name_ = s_new;

	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgSpace::addLayer					( PgLayer * layer )
{
	PgLayGlob * glb_ly;
	PgLaySpc * loc_ly;
	int		idx;

	Q_ASSERT( layer != NULL );
	if ( layer->isGlobal() )
	{
		glb_ly = static_cast<PgLayGlob *>( layer );
		idx = glb_ly->globalIndex();
		glob_ly_list_.insert( idx, new PgItTree() );
	}
	else
	{
		loc_ly = static_cast<PgLaySpc *>( layer );
		idx = local_ly_lst_.indexOf( loc_ly );
		if ( idx != -1 )
		{
			Q_ASSERT( false );
			return false;
		}
		local_ly_lst_.append( loc_ly );
	}
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgItTree *		PgSpace::remGlobalLayer				( PgLayGlob * itm )
{
	int idx = itm->globalIndex();
	return remGlobalLayer( idx );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgSpace::setCurrentLayer			( PgLayer * new_val )
{
	if ( new_val == crt_layer_ )
		return true;

	if ( new_val != NULL )
	{
		Q_ASSERT( new_val->document() == document() );
		if ( new_val->isGlobal() == false )
		{
			PgLaySpc * spc_ly = static_cast<PgLaySpc *>(new_val);

			if ( spc_ly->space() != this )
			{
				Q_ASSERT( false );
				return false;
			}
		}
	}
	crt_layer_ = new_val;
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgLayer *			PgSpace::layerByName				(
		const QString & s_name, bool b_glb ) const
{
	foreach( PgLayer * itr, local_ly_lst_ )	{
		if ( QString::compare( itr->name(), s_name, Qt::CaseInsensitive ) == 0 )
			return itr;
	}
	if ( b_glb && ( assoc_doc_ != NULL ) )
	{
		return assoc_doc_->layerByName( s_name );
	}
	return NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
int					PgSpace::layerIndex					(
		PgLaySpc * layer ) const
{
	return local_ly_lst_.indexOf( layer );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString				PgSpace::layerUniqName				( void ) const
{
	int i;
	QString	s_name;
	for ( i = 0; i < 10000000; i++ )
	{
		s_name = QObject::tr( "Layer-%1" ).arg( i );
		if ( layerByName( s_name ) == NULL )
		{
			return s_name;
		}
	}
	return s_name;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				PgSpace::raiseSpaceCreated			( void )
{
	CpgLibrary::unique()->emitSpaceEmerging( this );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				PgSpace::raiseSpaceDeleted			( void )
{
	CpgLibrary::unique()->emitSpaceDeleted( this );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgItTree *			PgSpace::items						( PgLayGlob * layer )
{
	if ( layer == NULL )
		return NULL;
	if ( layer->document() != assoc_doc_ )
	{
		return NULL;
	}
	int i = layer->globalIndex();
	if ( ( i < 0 ) || ( i >= glob_ly_list_.count() ) )
		return NULL;
	return glob_ly_list_.at( i );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QRgb				PgSpace::qtSpecialForeGround		( PgCtx & ctx )
{
	/** @todo implement qtSpecialForeGround */
	Q_UNUSED( ctx );
	return 0;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QRgb				PgSpace::qtSpecialBackGround		( PgCtx & ctx )
{
	/** @todo implement qtSpecialBackGround */
	Q_UNUSED( ctx );
	return 0;
}
/* ========================================================================= */

///* ------------------------------------------------------------------------- */
//PropIndex *			PgSpace::properties					( int & count )
//{

//}
///* ========================================================================= */

///* ------------------------------------------------------------------------- */
//bool				PgSpace::setProp					(
//		PropIndex idx, const QString & value )
//{

//}
///* ========================================================================= */

///* ------------------------------------------------------------------------- */
//bool				PgSpace::setProp					(
//		PropIndex idx, qreal value )
//{

//}
///* ========================================================================= */

///* ------------------------------------------------------------------------- */
//bool				PgSpace::setProp					(
//		PropIndex idx, int value )
//{

//}
///* ========================================================================= */

///* ------------------------------------------------------------------------- */
//bool				PgSpace::setProp					(
//		PropIndex idx, void * value )
//{

//}
///* ========================================================================= */

///* ------------------------------------------------------------------------- */
//QVariantList		PgSpace::getProp					( PropIndex idx )
//{

//}
///* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
